package cn.hbu.zlfw.research.dao.impl;

import java.util.Map;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import cn.hbu.zlfw.common.base.dao.impl.BaseDaoImpl;
import cn.hbu.zlfw.common.util.PageModel;
import cn.hbu.zlfw.common.util.Utility;
import cn.hbu.zlfw.research.dao.ResearcherDao;
import cn.hbu.zlfw.research.entity.Researcher;

/**
 * 
 * @ClassName: ResearcherDaoImpl 
 * @Description: 科研人员dao接口实现
 * @author 王鹏伟<1223443289@qq.com>
 * @date 2017年10月23日 下午5:54:58  
 * @version V1.0
 */
@Repository("researcherDao")
public class ResearcherDaoImpl extends BaseDaoImpl<Researcher, String> implements ResearcherDao{
	
	/*
	 * (non-Javadoc)
	 * @see cn.hbu.zlfw.research.dao.ResearcherDao#findResearcherByParams(java.lang.String, java.lang.String)
	 */
	@Override
	public Researcher findResearcherByParams(Map<String,Object> params) {
		StringBuffer sbf=new StringBuffer();
		sbf.append("from Researcher where");
		for (Map.Entry<String, Object> me : params.entrySet()) {
			sbf.append(" "+me.getKey()+"=:"+me.getKey()+" and ");
		}
		sbf.append(" delState=:delState ");
		params.put("delState", 0);
		Query query=this.getSession().createQuery(sbf.toString());
		return (Researcher) Utility.queryParameterUtil(query, params).uniqueResult();
	}
	@Override
	public Object findByParams(Map<String,Object> params,Object obj) {
		StringBuffer sbf=new StringBuffer();
		String clazz = obj.getClass().getName();
		sbf.append("from "+clazz+" where");
		for (Map.Entry<String, Object> me : params.entrySet()) {
			sbf.append(" "+me.getKey()+"=:"+me.getKey()+" and ");
		}
		sbf.append(" delState=:delState ");
		params.put("delState", 0);
		Query query=this.getSession().createQuery(sbf.toString());
		return Utility.queryParameterUtil(query, params).uniqueResult();
	}
	/*
	 * (non-Javadoc)
	 * @see cn.hbu.zlfw.research.dao.ResearcherDao#listResearcherByParamsToRoot(cn.hbu.zlfw.common.util.PageModel, java.lang.String, java.lang.Integer)
	 */
	@Override
	public PageModel<Researcher> listResearcherByParamsToRoot(PageModel<Researcher> pageModel, String userName,
			Integer schoolType) {
		Map<String, Object> params =Utility.getMapInstance();
		params.put("delState", 0);	
		StringBuffer sbfhql = new StringBuffer();
		sbfhql.append("from Researcher where delState=:delState ");
		if(userName != null && userName != "") {
			params.put("userName", "%" + userName + "%");
			sbfhql.append("and userName like :userName ");
		}
		if(schoolType != null && schoolType != 0) {
			params.put("schoolType", schoolType);
			sbfhql.append("and schoolType = :schoolType ");
		}
		return this.findListByParams(sbfhql.toString(), null, params, pageModel);
	}
	/*
	 * (non-Javadoc)
	 * @see cn.hbu.zlfw.research.dao.ResearcherDao#getResearcherToUpdate(java.lang.String, java.lang.String)
	 */
	@Override
	public Researcher getResearcherToUpdate(String phoneNumber, String id) {
		Map<String, Object> params =Utility.getMapInstance();
		params.put("id", id);
		params.put("phoneNumber", phoneNumber);
		params.put("delState", 0);	
		String hql = "from Researcher where id != :id and phoneNumber=:phoneNumber and delState=:delState";
		Query query = this.getSession().createQuery(hql);
		return (Researcher) Utility.queryParameterUtil(query, params).uniqueResult();
	}

}
